<?php
/*
 * Affiche toutes les parties en cours
 */


$finishedGames = array();
$aliveGames = array();
$gameIds = getGameIds();
natsort($gameIds);

foreach($gameIds as $gameId){
	$game = loadGame($gameId);
	if($game['endOfGame'] != 0){
		$finishedGames[] = array('gameId' => $gameId, 'updateDate' => $game['update_date'], 'author' => $game['author'], 'players' => array_keys($game['players']));
	}else{
		$aliveGames[] = array('gameId' => $gameId, 'updateDate' => $game['update_date'], 'author' => $game['author'], 'players' => array_keys($game['players']));
	}
}
   
    
echo('Parties terminees :</br>');
echoGameLines($finishedGames);
echo '</br>';

echo('Parties toujours en cours :</br>');
echoGameLines($aliveGames);
echo '</br>';

    
    
   
function echoGameLines($games){
    if(count($games) > 0){
	    foreach($games as $gameTmp){
	    	$joueursStr = array();
	    	foreach($gameTmp['players'] as $playerId ){
	    		if($playerId != 'CPU'){
	    			$joueursStr[] = sprintf('<a href="%s/admin/view/player/%s">%s</a>', PATH_ROOT, $playerId, $playerId);
	    		}
	    	}
	    	echo(sprintf('[<a href="%s/admin/delete/game/%s">Supprimer</a>]  <a href="%s/admin/view/game/%s">%s</a> - Mise a jour : %s - Auteur : <a href="%s/admin/view/player/%s">%s</a> - Joueurs : %s.</br>'
	    	     , PATH_ROOT, $gameTmp['gameId']
	    	     , PATH_ROOT, $gameTmp['gameId'], $gameTmp['gameId']
	    	     , $gameTmp['updateDate']
	    	     , PATH_ROOT, $gameTmp['author']
	    	     , $gameTmp['author']
	    	     , implode('-', $joueursStr)
	    	     ));
	    }    
    }else{
    	echo('Aucune.</br>');	
    }	
	return ;
}

